using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace SAE.Controls
{
    public partial class RunQuery : UserControl
    {
        public RunQuery()
        {
            InitializeComponent();

			this.cbSearchScope.SelectedIndex = 0;
        }     

        private void btnSearch_Click(object sender, EventArgs e)
        {
			this.gvSearchResult.DataSource = null;
			this.gvSearchResult.Update();

            SPSite site = new SPSite(SAE.SettingConstants.SiteUrl);
            SPWeb web = site.OpenWeb(SAE.SettingConstants.WebUrl);

            SPSiteDataQuery query = new SPSiteDataQuery();
            query.Lists = this.tbListes.Text;
            query.ViewFields = this.tbViewFields.Text;
            query.Webs = this.tbWebs.Text;
            query.Query = this.rtbQuery.Text;
            query.RowLimit = UInt32.Parse("100");

			DataTable table = web.GetSiteData(query);
			this.gvSearchResult.DataSource = table;
        }

		private void cbSearchScope_SelectedIndexChanged(object sender, EventArgs e)
		{
			switch(this.cbSearchScope.SelectedIndex)
			{
				case 0:
					this.tbWebs.Text = "<Webs Scope='SiteCollection' />";
					break;
				case 1:
					this.tbWebs.Text = "<Webs Scope='Recursive' />";
					break;
				case 2:
					this.tbWebs.Text = "";
					break;
			}
		}
    }
}
